package com.ruoyi.fs.service.impl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.fs.mapper.FsAddressMapper;
import com.ruoyi.fs.domain.FsAddress;
import com.ruoyi.fs.service.IFsAddressService;

/**
 * 地址Service业务层处理
 *
 * @author ruoyi
 * @date 2024-08-25
 */
@Service
public class FsAddressServiceImpl implements IFsAddressService {
    @Autowired
    private FsAddressMapper fsAddressMapper;

    /**
     * 查询地址
     *
     * @param id 地址主键
     * @return 地址
     */
    @Override
    public FsAddress selectFsAddressById(Long id) {
        return fsAddressMapper.selectFsAddressById(id);
    }

    /**
     * 查询地址
     *
     * @param name 地址名称
     * @return 地址
     */
    public List<FsAddress> selectFsAddressByNameExact(String name) {
        return fsAddressMapper.selectFsAddressByName(name);
    }

    /**
     * 查询地址
     *
     * @param name 地址名称
     * @return 地址
     */
    public List<FsAddress> selectFsAddressByName(String name) {
        List<FsAddress> list = selectFsAddressByNameExact(name);
        if (!list.isEmpty()) {
            return list;
        }
        name = "%" + name + "%";
        return fsAddressMapper.selectFsAddressByName(name);
    }

    /**
     * 查询地址
     *
     * @param code 地址编码
     * @return 地址
     */
    public FsAddress selectFsAddressByCode(String code) {
        return fsAddressMapper.selectFsAddressByCode(code);
    }

    /**
     * 查询地址列表
     *
     * @param fsAddress 地址
     * @return 地址
     */
    @Override
    public List<FsAddress> selectFsAddressList(FsAddress fsAddress) {
        return fsAddressMapper.selectFsAddressList(fsAddress);
    }

    /**
     * 新增地址
     *
     * @param fsAddress 地址
     * @return 结果
     */
    @Override
    public int insertFsAddress(FsAddress fsAddress) {
        FsAddress old = fsAddressMapper.selectFsAddressByCode(fsAddress.getCode());
        if (old == null) {
            fsAddress.setCreateTime(DateUtils.getNowDate());
            return fsAddressMapper.insertFsAddress(fsAddress);
        } else {
            return 0;
        }
    }

    /**
     * 修改地址
     *
     * @param fsAddress 地址
     * @return 结果
     */
    @Override
    public int updateFsAddress(FsAddress fsAddress) {
        fsAddress.setUpdateTime(DateUtils.getNowDate());
        return fsAddressMapper.updateFsAddress(fsAddress);
    }

    /**
     * 批量删除地址
     *
     * @param ids 需要删除的地址主键
     * @return 结果
     */
    @Override
    public int deleteFsAddressByIds(Long[] ids) {
        return fsAddressMapper.deleteFsAddressByIds(ids);
    }

    /**
     * 删除地址信息
     *
     * @param id 地址主键
     * @return 结果
     */
    @Override
    public int deleteFsAddressById(Long id) {
        return fsAddressMapper.deleteFsAddressById(id);
    }
}
